//
//  AuthViewController.swift
//  Nairacup
//
//  Created by ec on 2024/9/19.
//

import UIKit
import CoreLocation

class AuthViewController: UIViewController {
    
    @IBOutlet weak var contactsLabel: UILabel!
    @IBOutlet weak var locationLabel: UILabel!
    @IBOutlet weak var photoLabel: UILabel!
    @IBOutlet weak var cameraLabel: UILabel!
    @IBOutlet weak var contactsBtn: UIButton!
    @IBOutlet weak var locationBtn: UIButton!
    @IBOutlet weak var photoBtn: UIButton!
    @IBOutlet weak var cameraBtn: UIButton!
    
    var locationManager: CLLocationManager!
    var contactsDenied: Bool = false
    var locationDenied: Bool = false
    var photoDenied: Bool = false
    var cameraDenied: Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()
        
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest

        checkContactsPermission()
        checkLocationPermission()
        checkPhotoLibraryPermission()
        checkCameraPermission()
    }
    
    func checkContactsPermission() {
        AuthTool.checkContactsPermission { status in
            if status == .denied {
                contactsDenied = true
            }else if status == .authorized {
                contactsLabel.textColor = .init(hex: "#00B57B")
                contactsBtn.isSelected = true
            }
        }
    }
    
    func checkLocationPermission() {
        AuthTool.checkLocationPermission { status in
            if status == .denied {
                locationDenied = true
            }else if status == .authorized {
                locationLabel.textColor = .init(hex: "#00B57B")
                locationBtn.isSelected = true
            }
        }
    }
    
    func checkPhotoLibraryPermission() {
        AuthTool.checkPhotoLibraryPermission { status in
            if status == .denied {
                photoDenied = true
            }else if status == .authorized {
                photoLabel.textColor = .init(hex: "#00B57B")
                photoBtn.isSelected = true
            }
        }
    }

    func checkCameraPermission() {
        AuthTool.checkCameraPermission { status in
            if status == .denied {
                cameraDenied = true
            }else if status == .authorized {
                cameraLabel.textColor = .init(hex: "#00B57B")
                cameraBtn.isSelected = true
            }
        }
    }
    
    func applyContactsAuth() {
        AuthTool.applyContactsAuth { [weak self] status in
            if status == .authorized {
                self?.contactsLabel.textColor = .init(hex: "#00B57B")
                self?.contactsBtn.isSelected = true
            }
        }
    }
    
    func applyPhotoAuth() {
        AuthTool.applyPhotoAuth { [weak self] status in
            if status == .authorized {
                self?.photoLabel.textColor = .init(hex: "#00B57B")
                self?.photoBtn.isSelected = true
            }
        }
    }
    
    func applyCameraAuth() {
        AuthTool.applyCameraAuth { [weak self] status in
            if status == .authorized {
                self?.cameraLabel.textColor = .init(hex: "#00B57B")
                self?.cameraBtn.isSelected = true
            }
        }
    }
    
    func showSettingAlert() {
        let alert = UIAlertController(title: nil, message: "Permission has been disabled. Do you want to go to the settings to enable permission？", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Go set it up", style: .default, handler: { _ in
            if let setting = URL(string: UIApplication.openSettingsURLString) {
                UIApplication.shared.open(setting, options: [:], completionHandler: nil)
            }
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        present(alert, animated: true)
    }
    
    @IBAction func actionBack(_ sender: Any) {
        dismiss(animated: true)
    }
    
    @IBAction func actionContacts(_ sender: Any) {
        if contactsBtn.isSelected { return }
        if contactsDenied {
            showSettingAlert()
            return
        }
        applyContactsAuth()
    }
    
    @IBAction func actionLocation(_ sender: Any) {
        if locationBtn.isSelected { return }
        if locationDenied {
            showSettingAlert()
            return
        }
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }
    
    @IBAction func actionPhoto(_ sender: Any) {
        if photoBtn.isSelected { return }
        if photoDenied {
            showSettingAlert()
            return
        }
        applyPhotoAuth()
    }
    
    @IBAction func actionCamera(_ sender: Any) {
        if cameraBtn.isSelected { return }
        if cameraDenied {
            showSettingAlert()
            return
        }
        applyCameraAuth()
    }
    
}

extension AuthViewController: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        locationLabel.textColor = .init(hex: "#00B57B")
        locationBtn.isSelected = true
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {

    }
}

